package com.myapplication.app.bean;


import android.graphics.*;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.*;

/**
 * Created by happy_000 on 9/18/2014.
 */
public class TryJsonUtil {

    public static String isJsonRight(String str){
        try {
            JSONObject jsonObject=new JSONObject(str);
        } catch (JSONException e) {
            try {
                JSONArray jsonArray=new JSONArray(str);
            } catch (JSONException e2) {
                e.printStackTrace();
                return "";
            }
        }
        return str;
    }


    /**********
     *
     * @param s
     * @param  //关键字
     * @return
     */
    public static List<Mess> getMessList(String s){
        List<Mess> list=new ArrayList<Mess>(0);
        try {
            JSONObject jsonObject=new JSONObject(s);

            JSONArray jsonArray=jsonObject.getJSONArray("msg");
            for (int i = 0; i <jsonArray.length() ; i++) {
                Mess prodect=new Mess();
                Map<String,Object> map=getMapFromJson(jsonArray.getJSONObject(i));
                list.add(getProdectFromMap(map));

            }
        } catch (JSONException e) {
               e.printStackTrace();
        }
        return list;
    }


    /*********
     *
     * @param map
     * @return
     */
    public static Mess getProdectFromMap(Map<String,Object> map){
        Mess Mess=new Mess();
        try {
            Mess.setId(map.get("id").toString());
            Mess.setPid(map.get("pid").toString());
            Mess.setTo_uid(map.get("to_uid").toString());
            Mess.setFrom_uid(map.get("from_uid").toString());
            Mess.setMessage(map.get("message").toString());
            Mess.setImg(map.get("img").toString());
            Mess.setDateline(map.get("dateline").toString());
            Mess.setIs_raed(map.get("is_read").toString());
            Mess.setStatus(map.get("status").toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Mess;
    }

    /************
     *
     * @param jsonObject
     * @return
     */
    public static Map<String,Object> getMapFromJson(JSONObject jsonObject) {
        Map<String,Object> map=new HashMap<String, Object>(0);
        try {
            Iterator<String> list = jsonObject.keys();
            while (list.hasNext()) {
                String key = list.next();
                map.put(key, jsonObject.getString(key));
            }

        } catch (JSONException e) {
            e.printStackTrace();
        }
        return map;

    }




    /**
     * 将图片截取为圆角图片
     * @param bitmap 原图片
     * @param ratio 截取比例，如果是8，则圆角半径是宽高的1/8，如果是2，则是圆形图片
     * @return 圆角矩形图片
     */
    public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, float ratio) {

        Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
                bitmap.getHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(output);

        final Paint paint = new Paint();
        final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
        final RectF rectF = new RectF(rect);

        paint.setAntiAlias(true);
        canvas.drawARGB(0, 0, 0, 0);
        canvas.drawRoundRect(rectF, bitmap.getWidth()/ratio,
                bitmap.getHeight()/ratio, paint);

        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
        canvas.drawBitmap(bitmap, rect, rect, paint);
        return output;
    }
}
